package com.design.cjm.behavioral.visitor.example;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Author: Jie Ming Chen
 * @Date: 2018/9/26
 * @Version 1.0
 */
public class ObjectStructure {

    private List<Element> elementList = new ArrayList<>();

    public void attach(Element element) {
        elementList.add(element);
    }

    public void detach(Element element) {

        Iterator<Element> it = elementList.iterator();

        while (it.hasNext()) {
            if (it.next().equals(element)) {
                it.remove();
            }
        }
    }

    public void accept(Visitor visitor) {

        for (Element e: elementList) {
            e.accept(visitor);
        }
    }
}
